package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.TimeTableClassBean;
import com.mentaleye.biz.CurriculumTempBiz;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.OaClassBiz;
import com.mentaleye.biz.TimeTableBiz;
import com.mentaleye.biz.TimeTableClassBiz;
import com.mentaleye.dal.model.OaClassExample;
import com.mentaleye.dal.model.OaClassExample.Criteria;
import com.mentaleye.dal.model.TimeTableClassKey;
import com.mentaleye.dal.model.TimeTableExample;
import com.mentaleye.enums.TimeTableEnums;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping(value = "/timetableclass")
public class TimeTableClassController {

	@Autowired private TimeTableClassBiz timeTableClassBiz;
	@Autowired private DictionaryBiz dictionaryBiz;
	@Autowired private OaClassBiz oaClassBiz;
	@Autowired private CurriculumTempBiz curriculumTempBiz;
	@Autowired private TimeTableBiz timeTableBiz;
	
	@RequestMapping(value = "/index")
	public ModelAndView index(){
		ModelAndView view = new ModelAndView("tc/timetableclass/timeTableClassList");
		Map<Integer,String> stateMap = dictionaryBiz.queryDictionaryByParent(TimeTableEnums.ISCONFIRM.getCode());
		view.addObject("stateMap", stateMap);
		view.addObject("state", JSONObject.toJSONString(stateMap));
		return view;
	}
	
	@RequestMapping(value = "/timeTableListData")
	public @ResponseBody String timeTableListData(CommonPage commonPage){
		timeTableClassBiz.queryByMap(commonPage);
		return commonPage.toSHowPage();
	}
	
	@RequestMapping(value = "/edit")
	public @ResponseBody String edit(@RequestParam(value="oaClassId") Integer oaClassId,
			@RequestParam(value="timeTableId") Integer timeTableId){
		TimeTableClassBean bean = new TimeTableClassBean();
		bean.setOaClassId(oaClassId);
		bean.setTimeTableId(timeTableId);
		bean.setRelationState(137);
		return JSONObject.toJSONString(timeTableClassBiz.modifyTimeTableClass(bean));
	}
	@RequestMapping(value = "/desc")
	public ModelAndView desc(@RequestParam(value="oaClassId") Integer oaClassId,
			@RequestParam(value="timeTableId") Integer timeTableId){
		ModelAndView view = new ModelAndView("tc/timetableclass/timeTableClassDesc");
		view.addObject("oaClassId", oaClassId);
		view.addObject("timeTableId", timeTableId);
		return view;
	}
	@RequestMapping(value = "/modifydesc")
	public @ResponseBody String desc(TimeTableClassBean bean){
		return JSONObject.toJSONString(timeTableClassBiz.modifyTimeTableClass(bean));
	}
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(@RequestParam(value="timetable") String timetable[]){
		List<TimeTableClassKey> keylist = new ArrayList<TimeTableClassKey>();
		TimeTableClassKey key = null;
		for (int i = 0; i < timetable.length; i++) {
			for (int j = i+1;j < timetable.length;) {
				if(i == 0 || i%2 == 0 ){
					key= new TimeTableClassKey();
					key.setTimeTableId(Integer.valueOf(timetable[i]));
					key.setOaClassId(Integer.valueOf(timetable[j]));
					keylist.add(key);
				}
				break;
			}
		}
		TimeTableExample example = new TimeTableExample();
		Integer timeTableId = null;
		for (TimeTableClassKey timeTableClassKey : keylist) {
			timeTableId = timeTableClassKey.getTimeTableId();
		}
		example.createCriteria().andTimeTableIdEqualTo(timeTableId);
		timeTableBiz.removeByExample(example);
		return JSONObject.toJSONString(timeTableClassBiz.removeTimeTableClassList(keylist));
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(){
		ModelAndView view = new ModelAndView("tc/timetableclass/timeTableClassAdd");
		List<Integer> oaclassIdList = new ArrayList<Integer>();
		List<TimeTableClassBean> data = timeTableClassBiz.queryTimeTableClassByExample(null);
		if(data!=null){
			for (TimeTableClassBean timeTableClassBean : data) {
				oaclassIdList.add(timeTableClassBean.getOaClassId());
			}
		}
		OaClassExample example = new OaClassExample();
		Criteria criteria = example.createCriteria();
		if(oaclassIdList.size()>0){
			criteria.andOaclassIdNotIn(oaclassIdList);
		}
		criteria.andOaclassStateNotEqualTo(0);
		view.addObject("oaClassList", oaClassBiz.queryOaclass(example));
		view.addObject("curriculumTempList", curriculumTempBiz.queryByExample(null));
		return view;
	}
	
	@RequestMapping(value = "/save")
	public @ResponseBody String save(TimeTableClassBean bean){
		bean.setRelationState(TimeTableEnums.NOTCONFIRM.getCode());
		return JSONObject.toJSONString(timeTableClassBiz.addTimeTableClass(bean));
	}
}
